package geppetto.cat.programs;


import geppetto.cat.alignments.Alignment;
import geppetto.cat.alignments.AlignmentEvaluator;
import geppetto.cat.alignments.AlignmentSymetrization;
import geppetto.cat.alignments.AlignmentsSet;
import geppetto.cat.corpus.BilingualCorpus;
import geppetto.cat.models.AbstractModel;

import java.io.IOException;



public class GetSymmetrizationPosteriorCurves {

	public static void getIntersectionCurves(BilingualCorpus corpus, byte sentenceSource, boolean projectPosteriors, 
			AbstractModel modelF,AbstractModel modelB, String grepString,
			String type){
		int[] sentences = new int[corpus.getNumSentences(sentenceSource)];
		for (int i = 0; i < sentences.length; i++) {
			sentences[i] = i;
		}
		AlignmentsSet predictedF = modelF.posteriorAlignments(sentences,
				sentenceSource, 1f , projectPosteriors,false);
		AlignmentsSet predictedB = modelB.posteriorAlignments(sentences,
				sentenceSource, 1f , projectPosteriors,false);
		
		for(int i = 0; i < predictedF.size(); i++){
			predictedF.get(i).resetPoints();
			predictedB.get(i).resetPoints();
		}
		int divisions = 100;
		for (int i = 1; i <= divisions; i++) {
			AlignmentsSet predicted = null;
			if(type.equalsIgnoreCase("softIntersection")){
				predicted = AlignmentSymetrization.softIntersection(predictedF, predictedB, 1f * i / divisions);			
			}else if(type.equalsIgnoreCase("softUnion")){
				predicted = AlignmentSymetrization.softUnion(predictedF, predictedB, 1f * i / divisions);
			}else{
				System.out.println("Uknown type exiting");
				System.exit(-1);
			}
			float[] results = AlignmentEvaluator.calculateMeasures(predicted,
					corpus.getAlignments(sentences, sentenceSource));
			for(Alignment a : predicted._alignments){
				a.resetPoints();
			}
			System.out.println(grepString + " " + 1f * i / divisions + " P: " + results[0] + " R: " + results[1]);
		}
	}

	
	
	
	public static void main(String[] args) throws IOException {
		String corpusDescription = args[0];
		int size = Integer.parseInt(args[1]);
		int maxSentenceSize = Integer.parseInt(args[2]);
		
		System.out.println("Corpus " + corpusDescription);
		System.out.println("Size " + size);
		System.out.println("Max Sent length " + maxSentenceSize);
		
		BilingualCorpus forwardCorpus = BilingualCorpus.getCorpusFromFileDescription(corpusDescription, size,maxSentenceSize);
		BilingualCorpus backwardCorpus = forwardCorpus.reverse();	
	
		String forwardModelDir = args[3];
		String backwardModelDir = args[4];
		String modelName1 = args[5];
		double epsilon1 = Double.parseDouble(args[6]);
		double slack1 = Double.parseDouble(args[7]);
		int maxStep1 = Integer.parseInt(args[8]);
		int maxProjecIter1 = Integer.parseInt(args[9]);
		

		System.out.println("Model1 ");
		System.out.println("Forward Model Dir " + forwardModelDir);
		System.out.println("Backward Model Dir " + backwardModelDir);
		System.out.println("Model Name " + modelName1);
		System.out.println("Epsilon " + epsilon1);
		System.out.println("Slack " + slack1);
		System.out.println("Max Step " + maxStep1);
		System.out.println("Max Project Iterations " + maxProjecIter1);
		
		AbstractModel[] models= GenericMethods.initializeModelsFullDir(forwardCorpus, backwardCorpus, modelName1, 
				forwardModelDir,backwardModelDir, 
				epsilon1, slack1, maxStep1, maxProjecIter1);
	
		System.out.println("no projec: ");
		getIntersectionCurves(forwardCorpus,BilingualCorpus.TEST_CORPUS, false, models[0], models[1], "fnp", "softUnion");

		System.out.println(" Projec: ");
		getIntersectionCurves(forwardCorpus,BilingualCorpus.TEST_CORPUS, true, models[0], models[1], "fp", "softUnion");

		
		
		
	}
	
}
